﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SlexForSl4 {

    public class Area : Grid {
        public Area(double len, Orientation o = System.Windows.Controls.Orientation.Vertical)
            : this() {
            switch (o) {
                case Orientation.Horizontal:
                    this.Width = len;
                    this.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                    break;
                case Orientation.Vertical:
                    this.Height = len;
                    this.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                    break;
                default:
                    break;
            }
            this.Orientation = o;
        }
        public Orientation Orientation { get; set; }
        public Area() {

        }
        public void AddRow(params Area[] ps) {
            this.Children.Clear();
            var len = ps.Length;
            this.RowDefinitions.Clear();
            for (var i = 0; i < len; i++) {
                if (ps[i].Height > 0.1d) {
                    this.RowDefinitions.Add(new RowDefinition { Height = new GridLength(ps[i].Height) });
                } else {
                    this.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0d, GridUnitType.Star) });
                }
                this.Children.Add(ps[i]);
                Grid.SetRow(ps[i], i);
            }
        }
        public void AddColum(params Area[] ps) {
            this.Children.Clear();
            var len = ps.Length;
            this.ColumnDefinitions.Clear();
            for (var i = 0; i < len; i++) {
                if (ps[i].Width > 0.1d) {
                    this.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(ps[i].Width) });
                } else {
                    this.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0d, GridUnitType.Star) });
                }
                this.Children.Add(ps[i]);
                Grid.SetColumn(ps[i], i);
            }
        }
        public void RemoveAll() {
            this.Children.Clear();
        }
        public Area this[params int[] index] {
            get {
                var lp = default(Area);
                int i = 0;
                var len = index.Length;
                if (len == 1) {
                    return this.Children[index[0]] as Area;
                }
                lp = this;
                while (i < len) {
                    if (lp != null) {
                        var itemx = lp[index[i]] as Area;
                        lp = itemx;
                        i++;
                    }
                }
                return lp;
            }
        }
        public virtual void LoadContent(FrameworkElement fe) {
            this.Children.Clear();
            this.ColumnDefinitions.Clear();
            this.RowDefinitions.Clear();
            this.Children.Add(fe);
        }
        public void LoadContent2(FrameworkElement fe) {
            this.ClearContent();
            fe.Margin = new Thickness(0);
            fe.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            fe.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            Area.SetColumn(fe, 0);
            Area.SetRow(fe, 0);
            Area.SetColumnSpan(fe, this.ColumnDefinitions.Count);
            Area.SetRowSpan(fe, this.RowDefinitions.Count);
            this.Children.Add(fe);
        }
        public void ClearContent() {
            foreach (var item in this.Children) {
                if (item is Area) {
                    continue;
                }
                this.Children.Remove(item);
            }
        }
    }
}
